/**
 * Copyright (c) 2020 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.GenDatasourceDTO;
import com.example.entity.DataSourceEntity;
import com.example.model.page.PageData;
import com.example.model.api.Result;
import com.example.config.DataSourceInfo;
import com.example.service.DataSourceService;
import com.example.utils.ConvertUtils;
import com.example.utils.DbUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 数据源管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("datasource")
@Tag(name = "数据源管理")
public class DataSourceController {
    @Autowired
    private DataSourceService datasourceService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<GenDatasourceDTO>> page(@RequestParam Map<String, Object> params){
        PageData<GenDatasourceDTO> page = datasourceService.page(params);

        return new Result<PageData<GenDatasourceDTO>>().ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<List<GenDatasourceDTO>> list(){
        List<GenDatasourceDTO> list = datasourceService.list();

        return new Result<List<GenDatasourceDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "详情")
    public Result<GenDatasourceDTO> get(@PathVariable("id") Long id){
        GenDatasourceDTO data = ConvertUtils.sourceToTarget(datasourceService.selectById(id), GenDatasourceDTO.class);

        return new Result<GenDatasourceDTO>().ok(data);
    }

    @GetMapping("test/{id}")
    @Operation(summary = "测试")
    public Result<String> test(@PathVariable("id") Long id){
        try {
            DataSourceEntity dataSourceEntity = datasourceService.selectById(id);
            Connection connection = DbUtils.getConnection(new DataSourceInfo(dataSourceEntity));
            connection.close();
            return new Result<String>().ok("连接成功");
        } catch (Exception e) {
            return new Result<String>().ok("连接失请检查配置信息" + e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "新增")
    public Result<String> save(@RequestBody GenDatasourceDTO genDatasourceDTO){
        ValidatorUtils.validateEntity(genDatasourceDTO, AddGroup.class, DefaultGroup.class);
        datasourceService.insert(ConvertUtils.sourceToTarget(genDatasourceDTO, DataSourceEntity.class));

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody GenDatasourceDTO genDatasourceDTO){
        ValidatorUtils.validateEntity(genDatasourceDTO, UpdateGroup.class, DefaultGroup.class);
        datasourceService.updateById(ConvertUtils.sourceToTarget(genDatasourceDTO, DataSourceEntity.class));

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody Long[] ids){
        AssertUtils.isListEmpty(Arrays.asList(ids), "不能为空");
        datasourceService.deleteBatchIds(Arrays.asList(ids));

        return new Result<String>().ok("ok");
    }
}